// components/time-picker/index.js
import {
  TimePickerModel
} from '../../models/timePick'
let timePickModel = new TimePickerModel()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    dateTimeArray: Array,
    dateTime: Array,
    formatTime: String,
    startYear:Number,
    endYear:Number,
    initDate: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    // 参考数据
    // initDate: '2018-10-01 00:00:00',
    // startYear: 2000,
    // endYear: 2050
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      // console.log("time picker component attached")
      var obj = timePickModel.dateTimePicker(this.data.startYear,this.data.endYear,this.data.initDate)
      this.setData({
        dateTimeArray: obj.dateTimeArray,
        dateTime: obj.dateTime,
        formatTime: timePickModel.formatTime(obj.dateTimeArray, obj.dateTime)
      })
      
      this.sendDateTime()
      // this.getfirstDay()
      
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    sendDateTime(){
      this.triggerEvent('sendTimeToFather', {
        formatTime: this.data.formatTime
      }, {})
      this.triggerEvent('sendTimeToFather1', {
        formatTime: this.data.formatTime
      }, {})
    },
    changeDate(e) {
      this.setData({
        date: e.detail.value
      });
    },
    changeTime(e) {
      this.setData({
        time: e.detail.value
      });
    },
    changeDateTime(e) {
      // console.log(e.detail.value)
      this.setData({
        dateTime: e.detail.value,
        formatTime: timePickModel.formatTime(this.data.dateTimeArray, e.detail.value)
      });
      this.sendDateTime()
      // console.log(this.data.formatTime)
    },
    changeDateTimeColumn(e) {
      var arr = this.data.dateTime,
        dateArr = this.data.dateTimeArray;

      arr[e.detail.column] = e.detail.value;
      dateArr[2] = timePickModel.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

      this.setData({
        dateTimeArray: dateArr,
        dateTime: arr
      });
    },
    // 初始化开始时间为每月第一天
    getfirstDay() {
      var date = new Date();
      var firstDay = util.formatTime(new Date(date.getFullYear(), date.getMonth(), 1));
      // console.log(firstDay)
      var format = firstDay.replace(/\//g, '-')
      this.setData({
        initDate: format + ""
      })
    }
  }
})
